package com.fulihui.information.service;

import com.fulihui.information.api.AppObtainTypeService;
import com.fulihui.information.dal.dataobj.InfoAppObtainType;
import com.fulihui.information.dal.dataobj.InfoAppObtainTypeExample;
import com.fulihui.information.dal.mapper.InfoAppObtainTypeMapper;
import com.fulihui.information.dto.AppObtainTypeDTO;
import com.fulihui.information.enums.EAppType;
import com.fulihui.information.request.AppQueryObtainTypeIdRequest;
import com.fulihui.information.request.AppQueryObtainTypeRequest;
import com.fulihui.information.request.AppQueryObtainTypeSaveRequest;
import org.apache.dubbo.config.annotation.Service;
import org.near.servicesupport.error.Errors;
import org.near.servicesupport.result.BaseResult;
import org.near.servicesupport.result.TMultiResult;
import org.near.servicesupport.result.TSingleResult;
import org.near.servicesupport.util.ServiceAssert;
import org.near.toolkit.common.EnumUtil;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;
import java.util.stream.Collectors;

import static java.util.Collections.emptyList;
import static org.near.servicesupport.result.ResultBuilder.*;
import static org.near.toolkit.common.StringUtil.isNotBlank;
import static org.springframework.util.CollectionUtils.isEmpty;

/**
 * @author Willard.Hu on 2017/1/24 0024.
 */
@Service(version = "1.0.0")
 public class AppObtainTypeServiceImpl implements AppObtainTypeService {
    @Autowired
    private InfoAppObtainTypeMapper infoAppObtainTypeMapper;

    /**
     * @see AppObtainTypeService#getObtainType(AppQueryObtainTypeRequest)
     */
    @Override
    public TSingleResult<AppObtainTypeDTO> getObtainType(AppQueryObtainTypeRequest request) {
        // 校验参数
        ServiceAssert.notBlank(request.getVersion(), Errors.Commons.REQUEST_PARAMETER_ERROR);
        ServiceAssert.isTrue(EnumUtil.inEnum(request.getAppType(), EAppType.class),
                Errors.Commons.REQUEST_PARAMETER_ERROR);

        InfoAppObtainTypeExample example = new InfoAppObtainTypeExample();
        example.createCriteria().andVersionEqualTo(request.getVersion())
                .andAppTypeEqualTo(request.getAppType());
        List<InfoAppObtainType> list = infoAppObtainTypeMapper.selectByExample(example);
        AppObtainTypeDTO dto = null;
        if (!isEmpty(list)) {
            dto = toDTO(list.get(0));
        }
        return succTSingle(dto);
    }

    /**
     * @see AppObtainTypeService#query(AppQueryObtainTypeRequest)
     */
    @Override
    public TMultiResult<AppObtainTypeDTO> query(AppQueryObtainTypeRequest request) {
        InfoAppObtainTypeExample example = new InfoAppObtainTypeExample();
        InfoAppObtainTypeExample.Criteria criteria = example.createCriteria();
        if (isNotBlank(request.getAppType())) {
            criteria.andAppTypeEqualTo(request.getAppType());
        }
        if (isNotBlank(request.getVersion())) {
            criteria.andVersionEqualTo(request.getVersion());
        }
        List<InfoAppObtainType> list = infoAppObtainTypeMapper.selectByExample(example);

        TMultiResult<AppObtainTypeDTO> res;
        if (!isEmpty(list)) {
            res = succTMulti(list.parallelStream()
                    .map(AppObtainTypeServiceImpl::toDTO)
                    .collect(Collectors.toList()));
        } else {
            res = succTMulti(emptyList());
        }
        return res;
    }

    /**
     * @see AppObtainTypeService#save(AppQueryObtainTypeSaveRequest)
     */
    @Override
    public BaseResult save(AppQueryObtainTypeSaveRequest request) {
        InfoAppObtainType record = toRecord(request);
        boolean b = true;
        if (request.getId() != null) {
            b = infoAppObtainTypeMapper.updateByPrimaryKey(record) > 0;
        } else {
            infoAppObtainTypeMapper.insert(record);
        }
        return b ? succ() : fail(Errors.Commons.SYSTEM_ERROR);
    }

    /**
     * @see AppObtainTypeService#delete(AppQueryObtainTypeIdRequest)
     */
    @Override
    public BaseResult delete(AppQueryObtainTypeIdRequest request) {
        boolean b = infoAppObtainTypeMapper.deleteByPrimaryKey(request.getId()) > 0;
        return b ? succ() : fail(Errors.Commons.SYSTEM_ERROR);
    }

    private static AppObtainTypeDTO toDTO(InfoAppObtainType record) {
        if (record == null) {
            return null;
        }
        AppObtainTypeDTO dto = new AppObtainTypeDTO();
        dto.setId(record.getId());
        dto.setAppType(record.getAppType());
        dto.setVersion(record.getVersion());
        dto.setObtainType(record.getObtainType());
        return dto;
    }

    private static InfoAppObtainType toRecord(AppQueryObtainTypeSaveRequest request) {
        if (request == null) {
            return null;
        }
        InfoAppObtainType record = new InfoAppObtainType();
        record.setId(request.getId());
        record.setAppType(request.getAppType());
        record.setVersion(request.getVersion());
        record.setObtainType(request.getObtainType());
        return record;
    }
}
